package com.clock.service.impl;


import com.clock.common.constant.HttpStatus;
import com.clock.common.domain.R;
import com.clock.common.exception.BizException;
import com.clock.common.utils.LocalDateTimeUtil;
import com.clock.domain.dto.CommentDTO;
import com.clock.domain.entity.Comment;
import com.clock.mapper.CommentMapper;
import com.clock.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Transactional
@Service
public class CommentServiceImpl implements CommentService {

    /**
     * 帖子评论
     */
    private static final Integer COMMENT_TYPE_POST = 1;

    /**
     * 回复评论
     */
    private static final Integer COMMENT_TYPE_REPLY = 2;



    @Resource
    CommentMapper commentMapper;

    @Override
    public R addNewComment(CommentDTO commentDTO) {


        //判断是否为空
        if (commentDTO == null) {
            throw new BizException(HttpStatus.BAD_REQUEST, "参数异常");
        }

        try {
            //获取创建时间
            LocalDateTime localDateTime = LocalDateTimeUtil.getLocalDateTime();
            Date createTime = LocalDateTimeUtil.LocalDateTimeToDate(localDateTime);
            commentDTO.setCreateTime(createTime);

            //插入数据库
            commentMapper.insertComment(commentDTO);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new BizException(HttpStatus.ERROR, "评论失败");
        }

        return R.ok("评论成功");
    }


    /**
     * 通过评论实体id查询对帖子的评论
     *
     * @param entityId 实体id
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Comment> getCommentsByLimit(Long entityId, Integer pageNum, Integer pageSize) {

        if (entityId == null || pageNum == null || pageSize == null) {
            throw new BizException(HttpStatus.BAD_REQUEST, "请求参数异常");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> list = commentMapper.selectCommentsByEntityIdAndType(entityId, COMMENT_TYPE_POST);
        PageInfo<Comment> listInfo = null;
        if (list != null) {
            listInfo = new PageInfo<>();
            listInfo.setList(list);
        }

        listInfo.setTotal(list.size());

        return listInfo;
    }

    /**
     * 通过评论实体id查询回复评论
     * @param entityId 实体id
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Comment> getReplyCommentsByLimit(Long entityId, Integer pageNum) {

        PageHelper.startPage(pageNum, 5);
        List<Comment> list = commentMapper.selectCommentsByEntityIdAndType(entityId, COMMENT_TYPE_REPLY);
        PageInfo<Comment> listInfo = null;
        if (list != null) {
            listInfo = new PageInfo<>();
            listInfo.setList(list);
        }

        return listInfo;
    }

}
